from flask import Flask, request
from flask import render_template   # 自动查询templates文件夹里面的内容
from research import researchletters   # 调用自定义函数模块
from BMI import countbmi


app = Flask(__name__)


# <----------------------首页---------------------->
@app.route('/')
def hello():  # put application's code here
    return render_template('hello.html')


# <----------------------获取用户姓名页--------------------->
@app.route('/login', methods=['GET', 'POST'])
def login():
    return render_template('login.html')


# <----------------------获取用户姓名后的页面------------------------->
# 【POST】实现用户输入，点击提交，将数据提交到/result
@app.route('/index', methods=["POST"])
def index():  # put application's code here
    # 接收数据：request.form[HTML-参数名称]
    # 完整的应用：用户输入数据👉后段接收数据👉处理数据👉返回到HTML页面
    # if request.method=='POST':
    first_name = request.form['firstname']
    last_name = request.form['lastname']
    full_name = first_name.title() + ' ' + last_name.title()
    # if first_name == "Kaijun" and last_name == "Yu":
    return render_template("index.html",
                           fullname=full_name)


# <------------------元音字母------------------------->
@app.route('/vowel', methods=['GET', 'POST'])
def vowel():
    return render_template('vowel.html')


@app.route('/vowel_result', methods=["POST", "GET"])
def vowel_result():
    py_word = request.form['word']
    py_vowels = request.form['vowels']
    py_found = researchletters(py_vowels, py_word)
    return render_template('vowel_result.html', found=py_found)


# <--------------------名字全称----------------------->
@app.route('/name', methods=['GET', 'POST'])
def name():
    return render_template('name.html')


@app.route('/name_result', methods=['GET', 'POST'])
def name_result():
    first_name = request.form['firstname']
    last_name = request.form['lastname']
    full_name = first_name.title() + ' ' + last_name.title()
    return render_template("name_result.html",
                           fullname=full_name)


# <-----------------------bmi计算--------------------------->
@app.route('/bmi', methods=['POST', 'GET'])
def bmi():
    return render_template('bmi.html')


@app.route('/bmi_result', methods=['POST', 'GET'])
def bmi_result():
    py_height = float(request.form['height'])
    py_weight = float(request.form['weight'])
    py_bmi, py_suggest = countbmi(py_weight, py_height)
    return render_template('bmi_result.html',
                           bmi=py_bmi, suggest=py_suggest)


# <-------------------------购物车-------------------------->
@app.route('/shop_home', methods=['GET', 'POST'])
def shop_home():
    return render_template('shop_home.html')


@app.route('/goods_list', methods=['POST', 'GET'])
def goods_list():

    py_username = request.form['username']
    # py_money = session.get('money')
    return render_template('goods_list.html',
                           user_name=py_username,)
                           # money=py_money)


@app.route('/shopping_cart', methods=['POST', 'GET'])
def shopping_cart():
    return render_template('shopping_cart.html')


@app.route('/goods_list2', methods=['POST', 'GET'])
def goods_list2():
    return render_template('goods_list.html')


@app.route('/settlement', methods=['POST'])
def settlement():
    # py_totalPrice = float(request.form['total_price'])
    # py_totalPricInt = int(py_totalPrice)
    # py_balance = float(request.form['balance'])
    # if py_balance < py_totalPricInt:
    #     py_yesornot = ("余额不足，支付失败！")
    # else:
    #     py_yesornot = ("支付成功!欢迎下次再来~")
    #     py_last = py_balance - py_totalPricInt
    return render_template('settlement.html',)
                           # last=py_last,
                           # yesornot=py_yesornot)



if __name__ == '__main__':
    app.run()
